package com.lgz10.dao;

import com.lgz10.entity.User;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public class UserDao {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void saveUser(User user) {
        this.sessionFactory.getCurrentSession().save(user);
    }
    public List<User> queryUser(){
        String a="select * from user";
        SQLQuery sqlQuery=this.sessionFactory.getCurrentSession().createSQLQuery(a);
        sqlQuery.addEntity(User.class);
        System.out.println(sqlQuery.list().size());
        return sqlQuery.list();
    }
    public void deleteUser(User user){
        this.sessionFactory.getCurrentSession().delete(user);
    }
    public void updateUser(User user){
        this.sessionFactory.getCurrentSession().update(user);
    }
    public User findById(Integer id){
        return this.sessionFactory.getCurrentSession().get(User.class,id);
    }
}
